package com.sina.microblog.ut.cases
{
	import com.sina.microblog.MicroBlog;
	import com.sina.microblog.events.MicroBlogErrorEvent;
	import com.sina.microblog.events.MicroBlogEvent;
	import com.sina.microblog.ut.utils.AssertResult;
	import com.sina.microblog.ut.utils.ResultDataUtils;
	import com.sina.microblog.ut.utils.URLRequestChecker;
	import com.sina.microblog.ut.utils.URLRequestParameters;
	import com.sina.microblog.utils.StringEncoders;
	
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	
	import org.flexunit.async.Async;

	[RunWith("org.flexunit.experimental.theories.Theories")]
	public class DirectMessageTest
	{
		private static const DIRECT_MESSAGE_URL_BASE:String = "http://api.t.sina.com.cn/direct_messages.xml";
		private static const DIRECT_MESSAGE_SENT_URL_BASE:String = "http://api.t.sina.com.cn/direct_messages/sent.xml";
		private static const DIRECT_MESSAGE_NEW_URL_BASE:String = "http://api.t.sina.com.cn/direct_messages/new.xml";
		private static const DIRECT_MESSAGE_DESTROY_URL_BASE:String = "http://api.t.sina.com.cn/direct_messages/destroy/";
		
		private static const TIME_OUT:uint = 500;	
		private static var mb:MicroBlog;
		private static var tester:String = "1231232";
		private static var origMessage:String = "测试消息，&消息bca==";
		private static var testMessage:String = StringEncoders.urlEncodeUtf8String(origMessage);
		private static var testMessageID:uint = 132323;
		
		[DataPoints]
		[ArrayElementType("uint")]
		public static var ids:Array = [ 0, 3002343];
		
		[DataPoints]
		[ArrayElementType("String")]
		public static var names:Array = [ null, "测试abc1="];
		
		public function DirectMessageTest()
		{
		}
		
		[BeforeClass]
		public static function setUp():void
		{
			if ( null == mb )
			{
				mb = new MicroBlog();
			}
		}
		
		//Test Direct Message Recieved.
		[Test(async,timeout="20000")]
		public function testDirectMessageReicieved():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			var url:String = DIRECT_MESSAGE_URL_BASE + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_DIRECT_MESSAGES_RECEIVED_RESULT, AssertResult.assertMessageArray, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadDirectMessagesReceived();
			URLRequestChecker.check(mb, req, ResultDataUtils.messageArray);			
			
		}
		[Test(async,timeout="20000")]
		public function testDirectMessageReicievedFail():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			var url:String = DIRECT_MESSAGE_URL_BASE + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogErrorEvent.LOAD_DIRECT_MESSAGES_RECEIVED_ERROR, AssertResult.assertError, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadDirectMessagesReceived();
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		[Theory]
		public function testDirectMessageReicievedTheory(sinceID:uint, maxID:uint, count:uint, page:uint):void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.makeQueryStr(sinceID, maxID, count, page);
			var url:String = DIRECT_MESSAGE_URL_BASE + params.toGetString();
			
			var req:URLRequest = new URLRequest(url);
			mb.loadDirectMessagesReceived(sinceID, maxID, count, page);
			URLRequestChecker.check(mb, req, null);
		}
		
		//Test Direct Message Sent.
		[Test(async,timeout="20000")]
		public function testDirectMessageSent():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			var url:String = DIRECT_MESSAGE_SENT_URL_BASE + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_DIRECT_MESSAGES_SENT_RESULT, AssertResult.assertMessageArray, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadDirectMessagesSent();
			URLRequestChecker.check(mb, req, ResultDataUtils.messageArray);			
			
		}
		[Test(async,timeout="20000")]
		public function testDirectMessageSentFail():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			var url:String = DIRECT_MESSAGE_SENT_URL_BASE + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogErrorEvent.LOAD_DIRECT_MESSAGES_SENT_ERROR, AssertResult.assertError, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadDirectMessagesSent();
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		[Theory]
		public function testDirectMessageSentTheory(sinceID:uint, maxID:uint, count:uint, page:uint):void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.makeQueryStr(sinceID, maxID, count, page);
			var url:String = DIRECT_MESSAGE_SENT_URL_BASE + params.toGetString();
			
			var req:URLRequest = new URLRequest(url);
			mb.loadDirectMessagesSent(sinceID, maxID, count, page);
			URLRequestChecker.check(mb, req, null);
		}
		
		//Test Direct Message New.
		[Test(async,timeout="20000")]
		public function testDirectMessageNew():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("id", tester);
			Async.handleEvent(this, mb, MicroBlogEvent.SEND_DIRECT_MESSAGE_RESULT, AssertResult.assertMessage, TIME_OUT);
			
			var req:URLRequest = new URLRequest(DIRECT_MESSAGE_NEW_URL_BASE);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			req.data.text = testMessage;
			mb.sendDirectMessage(tester, origMessage);
			URLRequestChecker.check(mb, req, ResultDataUtils.message);			
			
		}
		[Test(async,timeout="20000")]
		public function testDirectMessageNewFail():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("id", tester);
			Async.handleEvent(this, mb, MicroBlogErrorEvent.SEND_DIRECT_MESSAGE_ERROR, AssertResult.assertError, TIME_OUT);
			
			var req:URLRequest = new URLRequest(DIRECT_MESSAGE_NEW_URL_BASE);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			req.data.text = testMessage;
			mb.sendDirectMessage(tester, origMessage);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		[Theory]
		public function testDirectMessageNewTheory(userId:uint, screenName:String):void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("id", tester);
	
			if ( userId > 0 )
			{
				params.addParam("user_id", userId);
			}
			if ( null != screenName )
			{
				params.addParam("screen_name", screenName);
			}
			
			var url:String = DIRECT_MESSAGE_SENT_URL_BASE + params.toGetString();
			
			var req:URLRequest = new URLRequest(DIRECT_MESSAGE_NEW_URL_BASE);
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			req.data.text = testMessage;
			mb.sendDirectMessage(tester, origMessage, userId, screenName);
			URLRequestChecker.check(mb, req, null);
		}
		
		//Test Direct Message New.
		[Test(async,timeout="20000")]
		public function testDirectMessageDestroy():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			Async.handleEvent(this, mb, MicroBlogEvent.DELETE_DIRECT_MESSAGE_RESULT, AssertResult.assertMessage, TIME_OUT);
			
			var req:URLRequest = new URLRequest(DIRECT_MESSAGE_DESTROY_URL_BASE+testMessageID.toString()+".xml");
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			mb.deleteDirectMessage(testMessageID);
			URLRequestChecker.check(mb, req, ResultDataUtils.message);			
			
		}
		[Test(async,timeout="20000")]
		public function testDirectMessageDestroyFail():void
		{
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			Async.handleEvent(this, mb, MicroBlogErrorEvent.DELETE_DIRECT_MESSAGE_ERROR, AssertResult.assertError, TIME_OUT);
			
			var req:URLRequest = new URLRequest(DIRECT_MESSAGE_DESTROY_URL_BASE+testMessageID.toString()+".xml");
			req.method = URLRequestMethod.POST;
			req.data = params.toPostData();
			mb.deleteDirectMessage(testMessageID);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
		}
		
	}
}